package com.hnevc.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Administrator on 2017/12/1.
 */

public class MyService extends Service {
    private static final String TAG = "MyService";
    private MediaPlayer mediaPlayer;

    public MyService() {

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        mediaPlayer = MediaPlayer.create(this,R.raw.qinglang);
        Log.e(TAG, "onBind: ");
        return new MyBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind: ");
        return true;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate: ");
        //构建一个线程，然后发送每隔0.5,秒发送一个广播，在广播中携带进度信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //发送广播
                    Intent intent = new Intent();
                    intent.setAction("com.hnevc.servicedemo.PROGRESS");
                    //TODO 携带数据
                    if(mediaPlayer!=null){
                        //播放器所要播放的资源时长
                        int duration = mediaPlayer.getDuration();
                        int currenPosition = mediaPlayer.getCurrentPosition();
                        intent.putExtra("duration",duration);
                        intent.putExtra("currenPosition",currenPosition);
                    }
                    sendBroadcast(intent);
                }
            }
        }).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }

    //定义绑定类
    class MyBinder extends Binder {
        //定义方法，供Activity调用
        public void call(String command) {
            Log.e(TAG, "call: " + command);
            if (command.equals("PLAY")) {
                play();
            }
            if (command.equals("PAUSE")) {
                pause();
            }
            if (command.equals("STOP")) {
                stop();
            }
        }
    }

    private void stop() {
        if(mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            //停止
            Log.e(TAG, "stop: ");
        }
    }

    private void pause() {
        if(mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            //暂停
            Log.e(TAG, "pause: ");
        }
    }

    private void play() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
            Log.e(TAG, "call: " + "正在播放");
        }else{
            Log.e(TAG, "play: "+"已经播放了" );
        }
    }
}
